package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.query.BpmnInfoQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.file.FileUploadUtils;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;

@Controller
@RequestMapping("/business/bpmnInfo")
public class BpmnInfoController {
    private static final String prefix = "business/bpmnInfo/";

    @Resource
    private IBpmnInfoService bpmnInfoService;

    @RequiresPermissions("business:bpmnInfo:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("business:bpmnInfo:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<BpmnInfo> query(BpmnInfoQuery qo) {
        return bpmnInfoService.query(qo);
    }

    @RequiresPermissions("business:bpmnInfo:deploy")
    @RequestMapping("/deployPage")
    public String deployPage() {
        return prefix + "deploy";
    }

    // 部署
    @RequiresPermissions("business:bpmnInfo:deploy")
    @RequestMapping("/deploy")
    @ResponseBody
    public AjaxResult deploy(String uploadPath, BpmnInfo bpmnInfo) throws Exception {
        bpmnInfoService.save(uploadPath, bpmnInfo);
        return AjaxResult.success();
    }

    // 上传
    @RequiresPermissions("business:bpmnInfo:upload")
    @RequestMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file) throws IOException {
        String uploadPath;
        if (file != null && file.getSize() > 0) {
            String filename = file.getOriginalFilename();
            String extension = FilenameUtils.getExtension(filename);
            if ("bpmn".equalsIgnoreCase(extension) || "zip".equalsIgnoreCase(extension)) {
                uploadPath = FileUploadUtils.upload(SystemConfig.getUploadPath(), file);
            } else {
                throw new BusinessException("上传文件类型有误");
            }
        } else {
            throw new BusinessException("不允许上传空文件");
        }
        return AjaxResult.success("上传成功", uploadPath);
    }

    // 查看流程文件和流程图
    @RequiresPermissions("business:bpmnInfo:readResource")
    @RequestMapping("/readResource")
    public void readResource(String deploymentId, String type, HttpServletResponse response) throws IOException {
        IOUtils.copy(bpmnInfoService.readResource(deploymentId, type), response.getOutputStream());
    }

    // 删除
    @RequiresPermissions("business:bpmnInfo:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(@ModelAttribute("ids") Long id) {
        bpmnInfoService.deleteById(id);
        return AjaxResult.success();
    }

}


